<?php
	namespace Admin\Controller;

	

	class GoodsController extends CommonController{
		public $like;
		public function index(){
			//查询数据
			$model=D('goods');
			$total=$model->wcount();
			$page=new \Think\Page($total,3);
			$page->setConfig('prev','上一页');
			$page->setConfig('next','下一页');
			$page_str=$page->show();
			$limit=$page->firstRow.','.$page->listRows;
			$list=$model->getAll($limit);
			foreach ($list as &$val) {
				$val['giname']='/Public/Uploads/'.$this->sub($val['giname']).'80_'.$val['giname'];
			}
			//发送数据
			$this->assign('page',$page_str);
			$this->assign('list',$list);

			//加载模板
			$this->display();
		}

		public function deleted(){
			$id=$_GET['id'];
			$goods = M('goods');

			$goods->startTrans();
				$res=$goods->where('id='.$id)->delete();

				if($res){
					$goods_img = M('goods_img');
					$resu=$goods_img->where('goods_id='.$id)->delete();
				}

				if($resu){
					$goods->commit();
					$this->success('删除成功',U('Goods/index'),2);
				}else{
					$goods->rollback();
					$this->error('删除失败');
				}
		}

		public function add(){
			if(empty($_POST)){
				$model=D('goods');
				$list=$model->showadd();
				$this->assign('list',$list);
				$this->display();
				return false;
			}
			
			$filename=$this->upload();
			$model=D('goods');
			$data=$model->create();
			if(!$data){
				$this->error($model->getError());
			}else{
				$data['describe']=$_POST['des'];
				$data['cate_id']=$_POST['cate'];
				$data['user_id']=$_SESSION['admin']['id'];
				
				$res=$model->setAdd($data,$filename);
				if($res){
					$this->success('添加成功',U('Goods/Index'),2);
					return false;
				}else{
					$this->setunlink($filename);
					$this->error('添加失败');
				}
			}

		}
			//进行文件上传
		public function upload(){   
			 $upload = new \Think\Upload();
			// 实例化上传类    
			$upload->maxSize   =     3145728 ;
			// 设置附件上传大小    
			$upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');


			//设置附件上传类型
			$upload->rootPath  =      './Public/'; 
			$upload->savePath  =      './Uploads/'; 
			$upload->saveName  =   date('Ymd',time()).uniqid();
			$upload->autoSub   = true;
			$upload->subName  =  date('Y',time()).'/'.date('m',time()).'/'.date('d',time());

			//设置附件上传目录
			//上传文件
			$info = $upload->upload();
			if(!$info){
				$this->error($upload->getError());
			}else{// 上传成功
				//做图片压缩方法
				$this->zoom($info['pic']['savename']);
				return $info['pic']['savename'];
			}
		}
		private function sub($str)
		{
			return substr($str,0,4).'/'.substr($str,4,2).'/'.substr($str,6,2).'/';
		}
		private function zoom($filename)
		{
			$image = new \Think\Image();
			$path='./Public/Uploads/'.$this->sub($filename);
			$image->open($path.$filename);
			$image->thumb(250,250)->save($path.'250_'.$filename);
			$image->thumb(150,150)->save($path.'150_'.$filename);
			$image->thumb(300,300)->save($path.'300_'.$filename);
			$image->thumb(80,80)->save($path.'80_'.$filename);
		}


		private function setunlink($filename)
		{
			$path='./Public/Uploads';
			unlink($path.$this->sub($filename).'150_'.$filename);
			unlink($path.$this->sub($filename).'250_'.$filename);
			unlink($path.$this->sub($filename).'300_'.$filename);
			unlink($path.$this->sub($filename).'80_'.$filename);
		}	

		public function like(){
			if(empty($this->like))
			$this->like=$_GET['like'];
			
			$model=D('goods');
			$total=$model->wcount($this->like);

			$page=new \Think\Page($total,3);
			$page->setConfig('prev','上一页');
			$page->setConfig('next','下一页');
			$page_str=$page->show($this->like);

			$limit=$page->firstRow.','.$page->listRows;
			$list=$model->getlike($this->like,$limit);
			foreach ($list as &$val) {
				$val['giname']='/Public/Uploads/'.$this->sub($val['giname']).'80_'.$val['giname'];
			}
			//发送数据
			$this->assign('page',$page_str);
			
			$this->assign('like',$this->like);
			$this->assign('list',$list);
			//加载模板
			$this->display();
		}
		public function Status(){
			
			$data=$_GET;
			foreach ($data as $key => $value) {
				if($key!=='id'){
					$k=$key;
					$status=$value;
				}else
				{
					$id=$value;
				}
			}
			$goodsModel=D('goods');

			$res=$goodsModel->change($k,$status,$id);

			$this->redirect('Goods/index');
		}

		public function More(){

			$id=$_GET['id'];
			$model=D('Goods');
			$list=$model->setmore($id);
			$list['giname']=__ROOT__.'/Public/Uploads/'.substr($list['giname'],0,4).'/'.substr($list['giname'],4,2).'/'.substr($list['giname'],6,2).'/80_'.$list['giname'];
	
			$this->assign('list',$list);

			$this->display();
		}

		public function doMore(){
			$gid=$_GET['id'];
			$data['describe']=$_POST['db'];
			$model=D('Goods');
			$res=$model->setdb($gid,$data);
			if($res){
				$this->success('添加描叙成功',U('index'),2);

			}else{
				$this->error('添加失败',U('index'));
				return false;
			}
		}
	}